<?php

/* Load widget with widget_init function */
add_action( 'widgets_init', 'recent_comments_widget' );

/* Register widget */
function recent_comments_widget()
{
	register_widget( 'recent_comments_widget' );
}

/* Handler class for all widget params */
class recent_comments_widget extends WP_Widget {

	/* Widget setup. */
	function recent_comments_widget()
	{
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'recent_comments_widget', 'description' => __('Recent Comments Widget.', 'recent_comments_widget') );
		/* Widget control settings. */
		$control_ops = array( 'width' => 250, 'height' => 250, 'id_base' => 'recent_comments_widget' );
		/* Create the widget. */
		$this->WP_Widget( 'recent_comments_widget', 'Laz - Recent Comments', $widget_ops, $control_ops );
	}

	/** Widget display template */
	function widget( $args, $instance )
	{
		extract( $args );

		/* Get widget settings */
		$title = apply_filters('widget_title', $instance['title'] );
		if ( isset($instance['id']) ) $id = $instance['id'];
		$max_comments = $instance['comment_count'];

		/* Widget themplate */
		echo $before_widget;
		
	   	if ( $title ) echo $before_title . $title . $after_title;

		echo '<div class="recent_comments">';
			$recent_comments = get_comments( array( 'number'    => $max_comments, 'status'    => 'approve') ); 
			echo '<ul>';
			
				$comment_count = 0;
			
				foreach($recent_comments as $recent_comment):
					$comment_count++;
					echo '<li class="clearfix">';
						echo '<div class="comment_image">';
							echo get_avatar($recent_comment->comment_author_email, 56);
						echo '</div>';
						echo '<div class="comment_content">';
							echo '<div class="author">';
								echo the_author_link(); echo ' > ';	echo $recent_comment->comment_author;
							echo '</div>';
							echo '<div class="commented_on">comments on</div>';
							echo '<div class="commented_item">';
								echo '<a href="'.get_permalink($recent_comment->comment_post_ID).'" title="'.get_the_title($recent_comment->comment_post_ID).'">'.get_the_title($recent_comment->comment_post_ID).'</a>';
							echo '</div>';
						echo '</div>';
					echo '</li>';
					if ( $comment_count == $max_comments ) break;	
				endforeach;
				
			echo '</ul>';
		echo '</div>';
		echo $after_widget;
	}

	/* Update widget settings. */
	function update( $new_instance, $old_instance )
	{
		$instance = $old_instance;

		/* Create widget settings instances. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['comment_count'] = $new_instance['comment_count'];
		return $instance;
	}

	/* Admin panel form  */
	function form( $instance )
	{
		$instance = wp_parse_args( (array) $instance );

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('title'); echo '">'; echo 'Widget Title'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('title'); echo '" name="'; echo $this->get_field_name('title'); echo '" value="'; if( isset($instance['title']) ) echo $instance['title']; echo '" />';
		echo '</p>';
        
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('comment_count'); echo '">'; echo 'Number of comments'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('comment_count'); echo '" name="'; echo $this->get_field_name('comment_count'); echo '" value="'; if( isset($instance['comment_count']) ) echo $instance['comment_count']; echo '" />';
		echo '</p>';
	}
}

?>